package com.cqupt.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqupt.model.pojo.ScoreRecord;
import com.cqupt.service.ScoreRecordService;
import com.cqupt.mapper.ScoreRecordMapper;
import com.cqupt.service.SparkService;
import com.cqupt.utils.AuthContextUtil;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ScoreRecordServiceImpl extends ServiceImpl<ScoreRecordMapper, ScoreRecord>
        implements ScoreRecordService {

    @Resource
    private SparkService sparkService;


    @Resource
    private ScoreRecordMapper scoreRecordMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public ScoreRecord scoreRecord() {
        Integer userId = AuthContextUtil.get().getId();
        LambdaQueryWrapper<ScoreRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ScoreRecord::getUserId, userId);
        List<ScoreRecord> scoreRecords = scoreRecordMapper.selectList(wrapper);
        if (ObjectUtil.isEmpty(scoreRecords)) {
            return null;
        }
        int total = 0;
        int integrity = 0;
        int fluency = 0;
        int accuracy = 0;

        for (ScoreRecord scoreRecord : scoreRecords) {
            accuracy += scoreRecord.getAccuracyScore();
            fluency += scoreRecord.getFluencyScore();
            integrity += scoreRecord.getIntegrityScore();
            total += scoreRecord.getTotalScore();
        }
        total /= scoreRecords.size();
        fluency /= scoreRecords.size();
        integrity /= scoreRecords.size();
        accuracy /= scoreRecords.size();
        ScoreRecord scoreRecord = new ScoreRecord();
        scoreRecord.setTotalScore(total);
        scoreRecord.setIntegrityScore(integrity);
        scoreRecord.setAccuracyScore(accuracy);
        scoreRecord.setFluencyScore(fluency);
        return scoreRecord;
    }

    @Override
    public String suggest(ScoreRecord scoreRecord) {
        Integer totalScore = scoreRecord.getTotalScore();
        Integer fluencyScore = scoreRecord.getFluencyScore();
        Integer integrityScore = scoreRecord.getIntegrityScore();
        Integer accuracyScore = scoreRecord.getAccuracyScore();

        Integer userId = AuthContextUtil.get().getId();
        String json = stringRedisTemplate.opsForValue().get("userId:" + userId + ":total:" + totalScore);
        if (!StrUtil.isEmpty(json)) {
            return json;
        }

        String str = "我现在发你一份英语口语的评测分数，包括以下几个部分：totalScore：" + totalScore + "；fluencyScore：" + fluencyScore +
                "；integrityScore：" + integrityScore + "；accuracyScore：" + accuracyScore + "。根据我给出的数据，生成三条学习建议，每条建议字数不超过20字。";
        String res = sparkService.chat(8888, str);
        stringRedisTemplate.opsForValue().set("userId:" + userId + ":total:" + totalScore, res);
        return res;
    }
}




